import axios from "axios";
import qs from "qs";

axios.defaults.timeout = 100000;

let apiUrl = "";
let hncWebUrl = "";
let masterUrl = "";
let cloudUrl = "";
switch (process.env.NODE_ENV) {
    case "development":
        // 本地服务器域名
        // apiUrl = "http://192.168.12.188:8021/cloudManagerApi"; // 本地测试用不能用https
        apiUrl = "https://apitest.hzncc.cn/cloudManagerApi";
        hncWebUrl = "https://apitest.hzncc.cn/HncWeb";
        // hncWebUrl = 'https://api.hzncc.cn/HncWeb';
        // hncWebUrl = "http://192.168.12.135:8067/HncWeb";
        cloudUrl = "https://apitest.hzncc.cn/index.php/api/Gcode";
        masterUrl = "https://maste.hzncc.cn";
        break;
    case "production_test":
        // 测试服务器域名
        apiUrl = "https://apitest.hzncc.cn/cloudManagerApi";
        hncWebUrl = "https://apitest.hzncc.cn/HncWeb";
        cloudUrl = "https://apitest.hzncc.cn/index.php/api/Gcode";
        masterUrl = "https://maste.hzncc.cn";
        break;
    case "production":
        apiUrl = "http://192.168.8.120:9000/cloudManagerApi";
        hncWebUrl = "http://192.168.8.120:9000/HncWeb";
        masterUrl = "https://maste.hzncc.cn";
        cloudUrl = "http://192.168.8.120:9000/index.php/api/Gcode";
        // 正式服务器域名
        // apiUrl = 'https://api.hzncc.cn/cloudManagerApi';
        // hncWebUrl = 'https://api.hzncc.cn/HncWeb';
        // masterUrl = 'https://maste.hzncc.cn';
        break;
}

// 添加请求拦截器: 让post请求的请求体格式为urlencoded格式 a=1&b=2
// 在真正发请求前执行
axios.interceptors.request.use(function(config) {
    // 得到请求方式和请求体数据
    const { method, data, headers } = config;
    let hasContentType = headers["Content-Type"];
    let isHeadersOfJson = headers["Content-Type"] && headers["Content-Type"] == "application/json";
    let isHeadersOfFormData = headers["Content-Type"] && headers["Content-Type"] == "multipart/form-data";
    let isHeadersOfUrlencoded = headers["Content-Type"] && headers["Content-Type"] == "application/x-www-form-urlencoded";
    let isObject = typeof data === "object";
    let isFormData = data instanceof FormData;
    if (method.toLowerCase() === "post") {
        if (isHeadersOfJson) {} else if (isHeadersOfFormData) {
            console.log("isHeadersOfFormData");
        } else if (isHeadersOfUrlencoded) {
            config.data = qs.stringify(data);
        } else {}
    }
    return config;
});

const cloudManagerGet = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        axios
            .get(apiUrl + url, {
                params: params
            })
            .then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    reject(err);
                }
            )
            .catch((error) => {
                reject(error);
            });
    });
};

// 针对云掌柜后面的页面；
const cloudManagerPost = (url, params = {}, config) => {
    return new Promise((resolve, reject) => {
        axios
            .post(apiUrl + url, params, config)
            .then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    reject(err);
                }
            )
            .catch((error) => {
                reject(error);
            });
    });
};

const hncWebGet = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        axios
            .get(hncWebUrl + url, {
                params: params
            })
            .then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    reject(err);
                }
            )
            .catch((error) => {
                reject(error);
            });
    });
};

// 针对云掌柜后面的hncWeb页面；
const hncWebPost = (url, params = {}, config) => {
    return new Promise((resolve, reject) => {
        axios
            .post(hncWebUrl + url, params, config)
            .then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    reject(err);
                }
            )
            .catch((error) => {
                reject(error);
            });
    });
};
const hnccloudGet = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        axios
            .get(cloudUrl + url, {
                params: params
            })
            .then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    reject(err);
                }
            )
            .catch((error) => {
                reject(error);
            });
    });
};

// 针对云掌柜后面的hncWeb页面；
const hnccloudPost = (url, params = {}, config) => {
    return new Promise((resolve, reject) => {
        axios
            .post(cloudUrl + url, params, config)
            .then(
                (response) => {
                    resolve(response.data);
                },
                (err) => {
                    reject(err);
                }
            )
            .catch((error) => {
                reject(error);
            });
    });
};
export default {
    cloudManagerGet,
    cloudManagerPost,
    hncWebGet,
    hncWebPost,
    hnccloudGet,
    hnccloudPost
};
